{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2习题 矩阵的输入及运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.1基础题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 1, 1]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 输入行向量b\n",
    "arr_b = np.array([[2, 1, 1]])\n",
    "arr_b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2进行谢列操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2],\n",
       "       [1],\n",
       "       [1]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求b的转置\n",
    "arr_b_tranpose = arr_b.T\n",
    "arr_b_tranpose"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 输入矩阵A\n",
    "arr_A = np.arange(1, 10).reshape(3, 3)\n",
    "arr_A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除第一行\n",
    "arr_A = np.delete(arr_A, obj=1, axis=0)\n",
    "arr_A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [7, 8, 9],\n",
       "       [2, 1, 1]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将b作为第三行元素加给A\n",
    "arr_A = np.append(arr_A, arr_b, axis=0)\n",
    "arr_A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[11 13 16]]\n",
      "[[ 7]\n",
      " [31]\n",
      " [ 6]]\n",
      "[[54 60 68]\n",
      " [60 69 79]\n",
      " [68 79 91]]\n"
     ]
    }
   ],
   "source": [
    "bA  = np.matmul(arr_b, arr_A)\n",
    "Ac  = np.matmul(arr_A, arr_b_tranpose)\n",
    "ATA = np.matmul(np.transpose(arr_A), arr_A)\n",
    "print(bA)\n",
    "print(Ac)\n",
    "print(ATA)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.18181818, 0.15384615, 0.125     ],\n",
       "       [0.09090909, 0.07692308, 0.0625    ],\n",
       "       [0.09090909, 0.07692308, 0.0625    ]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算对应位置元素的商\n",
    "result = np.divide(arr_b_tranpose, bA)\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  4,  9],\n",
       "       [49, 64, 81],\n",
       "       [ 4,  1,  1]], dtype=int32)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算个元素平方所组成的矩阵\n",
    "np.power(arr_A, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3计算向量之间欧式几何距离"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.58257569495584"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vecA = np.array([21, 22, 21, 19, 18])\n",
    "vecB = np.array([19, 21, 21, 23, 18])\n",
    "dist = np.sqrt(np.sum(np.square(vecA - vecB)))\n",
    "dist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 4输入一个在0到10之间的5维随机整数向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([8, 6, 2, 7, 0]), 0, 4, array([0, 6, 2, 7, 8]))"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vecA = np.random.randint(0, 10, size=(5))\n",
    "# 取出第一个最小元素和最后一个最大元素交换位置\n",
    "vecA_max_index , vecA_min_index= np.argmax(vecA), np.argmin(vecA)\n",
    "vecA_new = np.copy(vecA)\n",
    "vecA_new[vecA_max_index] = vecA[vecA_min_index]\n",
    "vecA_new[vecA_min_index] = vecA[vecA_max_index]\n",
    "vecA, vecA_max_index, vecA_min_index, vecA_new"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5求解线性方程组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.],\n",
       "       [ 2.],\n",
       "       [-3.],\n",
       "       [-0.],\n",
       "       [ 4.]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vecA = np.array([[23, 1, 8, -4, 6],\n",
    "                 [-3, 3, 6,  7, 1],\n",
    "                 [43, 2, 0,  9, -7],\n",
    "                 [4,  0, 9, -3, 5],\n",
    "                 [0, 3, -14, 9, 0]])\n",
    "vecB = np.array([[25], [-11], [19], [-3], [48]])\n",
    "np.linalg.solve(vecA, vecB)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.3 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "b09ec625f77bf4fd762565a912b97636504ad6ec901eb2d0f4cf5a7de23e1ee5"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
